library(rstan)
library(ggplot2)
library(gridExtra)
library(stargazer)

# Set to the local directory of the replication data
setwd("~/Downloads/Replication_Data/")

source("ggplot_theme.R")

A2016 <- readRDS("Data/A2016.rds")
A2012 <- readRDS("Data/A2012.rds")
A2008 <- readRDS("Data/A2008.rds")
A2004 <- readRDS("Data/A2004.rds")
A2000 <- readRDS("Data/A2000.rds")
A1996 <- readRDS("Data/A1996.rds")
A1992 <- readRDS("Data/A1992.rds")
A1988 <- readRDS("Data/A1988.rds")
A1984 <- readRDS("Data/A1984.rds")
A1980 <- readRDS("Data/A1980.rds")
A1976 <- readRDS("Data/A1976.rds")
A1972 <- readRDS("Data/A1972.rds")

A2016_dk <- readRDS("Data/A2016_dk.rds")
A2012_dk <- readRDS("Data/A2012_dk.rds")
A2008_dk <- readRDS("Data/A2008_dk.rds")
A2004_dk <- readRDS("Data/A2004_dk.rds")
A2000_dk <- readRDS("Data/A2000_dk.rds")
A1996_dk <- readRDS("Data/A1996_dk.rds")
A1992_dk <- readRDS("Data/A1992_dk.rds")
A1988_dk <- readRDS("Data/A1988_dk.rds")
A1984_dk <- readRDS("Data/A1984_dk.rds")
A1980_dk <- readRDS("Data/A1980_dk.rds")
A1976_dk <- readRDS("Data/A1976_dk.rds")
A1972_dk <- readRDS("Data/A1972_dk.rds")

posterior_anes_1972 <- readRDS("Fitted_Models/posterior_anes_1972.rds")
p_anes_1972 <- as.matrix(posterior_anes_1972)

posterior_anes_1976 <- readRDS("Fitted_Models/posterior_anes_1976.rds")
p_anes_1976 <- as.matrix(posterior_anes_1976)

posterior_anes_1980 <- readRDS("Fitted_Models/posterior_anes_1980.rds")
p_anes_1980 <- as.matrix(posterior_anes_1980)

posterior_anes_1984 <- readRDS("Fitted_Models/posterior_anes_1984.rds")
p_anes_1984 <- as.matrix(posterior_anes_1984)

posterior_anes_1988 <- readRDS("Fitted_Models/posterior_anes_1988.rds")
p_anes_1988 <- as.matrix(posterior_anes_1988)

posterior_anes_1992 <- readRDS("Fitted_Models/posterior_anes_1992.rds")
p_anes_1992 <- as.matrix(posterior_anes_1992)

posterior_anes_1996 <- readRDS("Fitted_Models/posterior_anes_1996.rds")
p_anes_1996 <- as.matrix(posterior_anes_1996)

posterior_anes_2000 <- readRDS("Fitted_Models/posterior_anes_2000.rds")
p_anes_2000 <- as.matrix(posterior_anes_2000)

posterior_anes_2004 <- readRDS("Fitted_Models/posterior_anes_2004.rds")
p_anes_2004 <- as.matrix(posterior_anes_2004)

posterior_anes_2008 <- readRDS("Fitted_Models/posterior_anes_2008.rds")
p_anes_2008 <- as.matrix(posterior_anes_2008)

posterior_anes_2012 <- readRDS("Fitted_Models/posterior_anes_2012.rds")
p_anes_2012 <- as.matrix(posterior_anes_2012)

posterior_anes_2016 <- readRDS("Fitted_Models/posterior_anes_2016.rds")
p_anes_2016 <- as.matrix(posterior_anes_2016)


a2016_vars <- c("place_dem_party", "place_rep_party", "place_hillary_clinton", "place_donald_trump")
a2016_names <- c("Democratic Party", "Republican Party", "Clinton", "Trump")
a2016_labels <- c("Democratic Party", "Republican Party", "Democratic candidate", "Republican candidate")

a2012_vars <- c("place_dem_party", "place_rep_party", "place_barack_obama", "place_mitt_romney")
a2012_names <- c("Democratic Party", "Republican Party", "Obama", "Romney")
a2012_labels <- c("Democratic Party", "Republican Party", "Democratic candidate", "Republican candidate")

a2008_vars <- c("place_dem_party", "place_rep_party", "place_barack_obama", "place_john_mccain")
a2008_names <- c("Democratic Party", "Republican Party", "Obama", "McCain")
a2008_labels <- c("Democratic Party", "Republican Party", "Democratic candidate", "Republican candidate")

a2004_vars <- c("place_dem_party", "place_rep_party", "place_john_kerry", "place_george_w_bush", "place_ralph_nader")
a2004_names <- c("Democratic Party", "Republican Party", "Kerry", "Bush", "Nader")
a2004_labels <- c("Democratic Party", "Republican Party", "Democratic candidate", "Republican candidate", "Nader")

a2000_vars <- c("place_dem_party", "place_rep_party", "place_al_gore", "place_george_w_bush", "place_bill_clinton", "place_reform_party", "place_pat_buchanan")
a2000_names <- c("Democratic Party", "Republican Party", "Gore", "Bush", "Clinton", "Reform Party", "Pat Buchanan")
a2000_labels <- c("Democratic Party", "Republican Party", "Democratic candidate", "Republican candidate", "Clinton", "Reform Party", "Pat Buchanan")

a1996_vars <- c("place_dem_party", "place_rep_party", "place_bill_clinton", "place_bob_dole", "place_ross_perot")
a1996_names <- c("Democratic Party", "Republican Party", "Clinton", "Dole", "Perot")
a1996_labels <- c("Democratic Party", "Republican Party", "Democratic candidate", "Republican candidate", "Perot")

a1992_vars <- c("place_dem_party", "place_rep_party", "place_bill_clinton", "place_george_hw_bush", "place_ross_perot")
a1992_names <- c("Democratic Party", "Republican Party", "Clinton", "Bush", "Perot")
a1992_labels <- c("Democratic Party", "Republican Party", "Democratic candidate", "Republican candidate", "Perot")

a1988_vars <- c("place_dem_party", "place_rep_party", "place_michael_dukakis", "place_george_hw_bush", "place_jesse_jackson", "place_ronald_reagan")
a1988_names <- c("Democratic Party", "Republican Party", "Dukakis", "Bush", "Jackson", "Reagan")
a1988_labels <- c("Democratic Party", "Republican Party", "Democratic candidate", "Republican candidate", "Jackson", "Reagan")

a1984_vars <- c("place_dem_party", "place_rep_party", "place_walter_mondale", "place_ronald_reagan")
a1984_names <- c("Democratic Party", "Republican Party", "Mondale", "Reagan")
a1984_labels <- c("Democratic Party", "Republican Party", "Democratic candidate", "Republican candidate")

a1980_vars <- c("place_dem_party", "place_rep_party", "place_jimmy_carter", "place_ronald_reagan", "place_ted_kennedy", "place_john_connelly", "place_gerald_ford", "place_jerry_brown", "place_john_anderson", "place_george_hw_bush", "place_walter_mondale", "place_patrick_lucey")
a1980_names <- c("Democratic Party", "Republican Party", "Carter", "Reagan", "Kennedy", "Connelly", "Ford", "Brown", "Anderson", "Bush", "Mondale", "Lucey")
a1980_labels <- c("Democratic Party", "Republican Party", "Democratic candidate", "Republican candidate", "Kennedy", "Connelly", "Ford", "Brown", "Anderson", "Bush", "Mondale", "Lucey")

a1976_vars <- c("place_dem_party", "place_rep_party", "place_jimmy_carter", "place_gerald_ford", "place_hubert_humphrey", "place_george_wallace", "place_george_mcgovern", "place_ronald_reagan", "place_walter_mondale", "place_bob_dole")
a1976_names <- c("Democratic Party", "Republican Party", "Carter", "Ford", "Humphrey", "Wallace", "McGovern", "Reagan", "Mondale", "Dole")
a1976_labels <- c("Democratic Party", "Republican Party", "Democratic candidate", "Republican candidate", "Humphrey", "Wallace", "McGovern", "Reagan", "Mondale", "Dole")

a1972_vars <- c("place_dem_party", "place_rep_party", "place_george_mcgovern", "place_richard_nixon", "place_george_wallace")
a1972_names <- c("Democratic Party", "Republican Party", "McGovern", "Nixon", "Wallace")
a1972_labels <- c("Democratic Party", "Republican Party", "Democratic candidate", "Republican candidate", "Wallace")

# Get summary statistics for the standard deviation of placements,
# the proportion don't knows, and the measure of uncertainty from the
# AM-scaling models
get_est <- function(data, data_dk, dk_value, posterior, vars, var_names, var_labels, year) {

  data.frame(year = year,
             item = var_names,
             label = var_labels,
             sd = apply(data[, vars], 2, sd, na.rm = TRUE),
             dk = apply(data_dk[, paste0(vars, "_dk")], 2, function(x) mean(x == dk_value, na.rm = TRUE)),
             dk_lower90 = apply(data_dk[, paste0(vars, "_dk")], 2, function(x) prop.test(sum(x == dk_value, na.rm = TRUE), sum(!is.na(x)))$conf.int[1]),
             dk_upper90 = apply(data_dk[, paste0(vars, "_dk")], 2, function(x) prop.test(sum(x == dk_value, na.rm = TRUE), sum(!is.na(x)))$conf.int[2]),
             sigma = apply(posterior[, grep("sigma\\[", colnames(posterior))], 2, median),
             sigma_lower90 = apply(posterior[, grep("sigma\\[", colnames(posterior))], 2, quantile, 0.05),
             sigma_upper90 = apply(posterior[, grep("sigma\\[", colnames(posterior))], 2, quantile, 0.95))

}

G1 <- rbind(get_est(A2016, A2016_dk, "-8. Don't know (FTF only)", p_anes_2016, a2016_vars, a2016_names, a2016_labels, 2016),
            get_est(A2012, A2012_dk, "-8. Don't know", p_anes_2012, a2012_vars, a2012_names, a2012_labels, 2012),
            get_est(A2008, A2008_dk, "-8. Don't know", p_anes_2008, a2008_vars, a2008_names, a2008_labels, 2008),
            get_est(A2004, A2004_dk, "8. Don't know", p_anes_2004, a2004_vars, a2004_names, a2004_labels, 2004),
            get_est(A2000, A2000_dk, "8. DK", p_anes_2000, a2000_vars, a2000_names, a2000_labels, 2000),
            get_est(A1996, A1996_dk, "8. DK", p_anes_1996, a1996_vars, a1996_names, a1996_labels, 1996),
            get_est(A1992, A1992_dk, "8. DK", p_anes_1992, a1992_vars, a1992_names, a1992_labels, 1992),
            get_est(A1988, A1988_dk, "8. DON-T KNOW", p_anes_1988, a1988_vars, a1988_names, a1988_labels, 1988),
            get_est(A1984, A1984_dk, "8. INCOMPLETE BRANC", p_anes_1984, a1984_vars, a1984_names, a1984_labels, 1984),
            get_est(A1980, A1980_dk, "8. DON-T KNOW", p_anes_1980, a1980_vars, a1980_names, a1980_labels, 1980),
            get_est(A1976, A1976_dk, "8. DON-T KNOW", p_anes_1976, a1976_vars, a1976_names, a1976_labels, 1976),
            get_est(A1972, A1972_dk, "8. DK", p_anes_1972, a1972_vars, a1972_names, a1972_labels, 1972))
row.names(G1) <- NULL

G1$year_color <- ifelse(G1$year %in% c(1984, 2016), G1$year, "Other")



###### Note to FIGURE 2:
# John Anderson (1980)
round(G1$sigma[G1$item == "Anderson" & G1$year == 1980], 2)
# Ross Perot (1992)
round(G1$sigma[G1$item == "Perot" & G1$year == 1992], 2)
# Ross Perot (1996)
round(G1$sigma[G1$item == "Perot" & G1$year == 1996], 2)
# Ralpha Nader (2004) (not asked in 2000)
round(G1$sigma[G1$item == "Nader" & G1$year == 2004], 2)


###### First panel of FIGURE 2:
# Whether the label goes to left or right of point estimate in the figure
G1$upper <- -1
for(year in seq(1972, 2016, by = 4)) {
  G1$upper[G1$year == year & G1$label == "Democratic candidate"] <- ifelse(G1$sigma[G1$year == year & G1$label == "Democratic candidate"] > G1$sigma[G1$year == year & G1$label == "Republican candidate"], 1, 0)
  G1$upper[G1$year == year & G1$label == "Republican candidate"] <- ifelse(G1$sigma[G1$year == year & G1$label == "Republican candidate"] > G1$sigma[G1$year == year & G1$label == "Democratic candidate"], 1, 0)
}

G1_plot <- ggplot(subset(G1, label %in% c("Democratic candidate", "Republican candidate")),
       aes(y = year, x = sigma, color = year_color, fill = as.factor(year))) +
  my.theme(borderless = 2, bordersize = 1, base_size = 10, tick_colour = "white", grid.x_colour = "grey85", grid.y_colour = "grey85") +
  coord_cartesian(xlim = c(0.65, 1.55)) +
  scale_y_continuous(breaks = seq(1972, 2016, by = 4)) +
  labs(y = "", x = expression("Ideological placement error ("*sigma[j]*")"),
       title = "Ideological Placement Error") +
  geom_line(size = 3.75) +
  geom_segment(data = subset(G1, year != 1984 & upper == 1), aes(y = year, yend = year, x = sigma_lower90, xend = sigma_upper90), lineend = "round", color = "grey20", size = 0.5) +
  geom_segment(data = subset(G1, year != 1984 & upper == 0), aes(y = year, yend = year, x = sigma_lower90, xend = sigma_upper90), lineend = "round", color = "grey20", size = 0.5) +
  geom_segment(data = subset(G1, year == 1984 & upper == 1), aes(y = year, yend = year, x = sigma_lower90, xend = sigma_upper90), lineend = "round", color = "grey70", size = 0.5) +
  geom_segment(data = subset(G1, year == 1984 & upper == 0), aes(y = year, yend = year, x = sigma_lower90, xend = sigma_upper90), lineend = "round", color = "grey70", size = 0.5) +
  geom_point(data = subset(G1, year != 1984 & upper %in% 0:1), shape = 21, fill = "white", color = "black", size = 3, stroke = 0.6) +
  geom_point(data = subset(G1, year == 1984 & upper %in% 0:1), shape = 21, fill = "white", color = "grey70", size = 3, stroke = 0.6) +
  geom_label(data = subset(G1, upper == 1 & year != 1984), aes(y = year, x = sigma, label = item), nudge_y = 1.6, nudge_x = 0.01, hjust = 0, size = 2.7, color = "black", fill = "grey97", label.r = unit(0, "lines"), label.padding = unit(0.15, "lines"), label.size = NA) +
  geom_label(data = subset(G1, upper == 0 & year != 1984), aes(y = year, x = sigma, label = item), nudge_y = 1.6, nudge_x = -0.01, hjust = 1, size = 2.7, color = "black", fill = "grey97", label.r = unit(0, "lines"), label.padding = unit(0.15, "lines"), label.size = NA) +
  geom_label(data = subset(G1, upper == 1 & year == 1984), aes(y = year, x = sigma, label = item), nudge_y = 1.6, nudge_x = 0.01, hjust = 0, size = 2.7, color = "grey50", fill = "grey97", label.r = unit(0, "lines"), label.padding = unit(0.15, "lines"), label.size = NA) +
  geom_label(data = subset(G1, upper == 0 & year == 1984), aes(y = year, x = sigma, label = item), nudge_y = 1.6, nudge_x = -0.01, hjust = 1, size = 2.7, color = "grey50", fill = "grey97", label.r = unit(0, "lines"), label.padding = unit(0.15, "lines"), label.size = NA) +
  geom_text(data = data.frame(y = 1986, x = 1.4),
            aes(y = y, x = sum(G1$sigma[G1$item %in% c("Reagan", "Mondale") & G1$year == 1984])/2,
              label = "*"), hjust = 0.5, size = 5, color = "black") +
  scale_color_manual(values = c("grey80", "pink", "grey80")) +
  theme(legend.position = "none",
        panel.background = element_rect(fill = "grey97", color = "white"),
        plot.margin = unit(c(0.5, 0.5, 0.5, -0.5), "lines"))

###### FIGURE A2:
# Difference of sigma
G2 <- data.frame(year = seq(1972, 2016, by = 4),
                 item = c("McGovern (Nixon)",
                          "Carter (Ford)",
                          "Carter (Reagan)",
                          "Mondale (Reagan)",
                          "Dukakis (Bush)",
                          "Bush (Clinton)",
                          "Clinton (Dole)",
                          "Bush (Gore)",
                          "Bush (Kerry)",
                          "Obama (McCain)",
                          "Obama (Romney)",
                          "Trump (Clinton)"),
                 sigma_diff = c(quantile(apply(p_anes_1972[, paste0("sigma[", 4:3, "]")], 1, diff), 0.5),
                                quantile(apply(p_anes_1976[, paste0("sigma[", 4:3, "]")], 1, diff), 0.5),
                                quantile(apply(p_anes_1980[, paste0("sigma[", 4:3, "]")], 1, diff), 0.5),
                                quantile(apply(p_anes_1984[, paste0("sigma[", 4:3, "]")], 1, diff), 0.5),
                                quantile(apply(p_anes_1988[, paste0("sigma[", 4:3, "]")], 1, diff), 0.5),
                                quantile(apply(p_anes_1992[, paste0("sigma[", 3:4, "]")], 1, diff), 0.5),
                                quantile(apply(p_anes_1996[, paste0("sigma[", 4:3, "]")], 1, diff), 0.5),
                                quantile(apply(p_anes_2000[, paste0("sigma[", 3:4, "]")], 1, diff), 0.5),
                                quantile(apply(p_anes_2004[, paste0("sigma[", 3:4, "]")], 1, diff), 0.5),
                                quantile(apply(p_anes_2008[, paste0("sigma[", 4:3, "]")], 1, diff), 0.5),
                                quantile(apply(p_anes_2012[, paste0("sigma[", 4:3, "]")], 1, diff), 0.5),
                                quantile(apply(p_anes_2016[, paste0("sigma[", 3:4, "]")], 1, diff), 0.5)),
                 lower90 = c(quantile(apply(p_anes_1972[, paste0("sigma[", 4:3, "]")], 1, diff), 0.05),
                             quantile(apply(p_anes_1976[, paste0("sigma[", 4:3, "]")], 1, diff), 0.05),
                             quantile(apply(p_anes_1980[, paste0("sigma[", 4:3, "]")], 1, diff), 0.05),
                             quantile(apply(p_anes_1984[, paste0("sigma[", 4:3, "]")], 1, diff), 0.05),
                             quantile(apply(p_anes_1988[, paste0("sigma[", 4:3, "]")], 1, diff), 0.05),
                             quantile(apply(p_anes_1992[, paste0("sigma[", 3:4, "]")], 1, diff), 0.05),
                             quantile(apply(p_anes_1996[, paste0("sigma[", 4:3, "]")], 1, diff), 0.05),
                             quantile(apply(p_anes_2000[, paste0("sigma[", 3:4, "]")], 1, diff), 0.05),
                             quantile(apply(p_anes_2004[, paste0("sigma[", 3:4, "]")], 1, diff), 0.05),
                             quantile(apply(p_anes_2008[, paste0("sigma[", 4:3, "]")], 1, diff), 0.05),
                             quantile(apply(p_anes_2012[, paste0("sigma[", 4:3, "]")], 1, diff), 0.05),
                             quantile(apply(p_anes_2016[, paste0("sigma[", 3:4, "]")], 1, diff), 0.05)),
                 upper90 = c(quantile(apply(p_anes_1972[, paste0("sigma[", 4:3, "]")], 1, diff), 0.95),
                             quantile(apply(p_anes_1976[, paste0("sigma[", 4:3, "]")], 1, diff), 0.95),
                             quantile(apply(p_anes_1980[, paste0("sigma[", 4:3, "]")], 1, diff), 0.95),
                             quantile(apply(p_anes_1984[, paste0("sigma[", 4:3, "]")], 1, diff), 0.95),
                             quantile(apply(p_anes_1988[, paste0("sigma[", 4:3, "]")], 1, diff), 0.95),
                             quantile(apply(p_anes_1992[, paste0("sigma[", 3:4, "]")], 1, diff), 0.95),
                             quantile(apply(p_anes_1996[, paste0("sigma[", 4:3, "]")], 1, diff), 0.95),
                             quantile(apply(p_anes_2000[, paste0("sigma[", 3:4, "]")], 1, diff), 0.95),
                             quantile(apply(p_anes_2004[, paste0("sigma[", 3:4, "]")], 1, diff), 0.95),
                             quantile(apply(p_anes_2008[, paste0("sigma[", 4:3, "]")], 1, diff), 0.95),
                             quantile(apply(p_anes_2012[, paste0("sigma[", 4:3, "]")], 1, diff), 0.95),
                             quantile(apply(p_anes_2016[, paste0("sigma[", 3:4, "]")], 1, diff), 0.95)))

G2$year_color <- ifelse(G2$year == 2016, G2$year, "Other")

# Whether label goes to left or right of point estimate in the figure
G2$upper <- 1
G2$upper[G2$item == "Trump (Clinton)"] <- 0

G2_plot <- ggplot(G2, aes(y = year, x = sigma_diff, fill = year_color, color = year_color)) +
  my.theme(borderless = 2, bordersize = 1, base_size = 10, tick_colour = "white", grid.x_colour = "grey85", grid.y_colour = "grey85") +
  coord_cartesian(xlim = c(-0.1, 0.45)) +
  scale_x_continuous(breaks = c(-0.1, 0, 0.1, 0.2, 0.3, 0.4),
                     labels = c("-0.1", "0", "0.1", "0.2", "0.3", "0.4")) +
  scale_y_continuous(breaks = seq(1972, 2016, by = 4)) +
  labs(y = "", x = expression("Difference in ideological placement error ("*sigma[j] - sigma[j^minute]*")"),
       title = "Difference in Ideological Placement Error") +
  geom_vline(xintercept = 0, linetype = 1, size = 0.3, color = "grey50") +
  geom_segment(aes(y = year, yend = year, x = lower90, xend = upper90), lineend = "round", color = "grey20", size = 0.5) +
  geom_point(shape = 21, size = 3, stroke = 0.6) +
  geom_label(data = subset(G2, upper == 1), aes(y = year, x = sigma_diff, label = item), hjust = 0.5, nudge_x = 0, nudge_y = 1.6, size = 2.7, color = "black", fill = "grey97", label.r = unit(0, "lines"), label.padding = unit(0.09, "lines"), label.size = NA) +
  geom_label(data = subset(G2, upper == 0), aes(y = year, x = sigma_diff, label = item), hjust = 0.5, nudge_x = 0, nudge_y = 1.6, size = 2.7, color = "black", fill = "grey97", label.r = unit(0, "lines"), label.padding = unit(0.09, "lines"), label.size = NA) +
  scale_fill_manual(values = c("pink", "grey80")) +
  scale_color_manual(values = c("black", "grey40")) +
  theme(legend.position = "none",
        panel.background = element_rect(fill = "grey97", color = "white"),
        plot.margin = unit(c(0.5, 0.5, 0.5, -0.5), "lines"))

###### Second panel to FIGURE 2:
# Ratio of sigma
G3 <- data.frame(year = seq(1972, 2016, by = 4),
                 item = c("McGovern (Nixon)",
                          "Carter (Ford)",
                          "Carter (Reagan)",
                          "Mondale (Reagan)",
                          "Dukakis (Bush)",
                          "Bush (Clinton)",
                          "Clinton (Dole)",
                          "Bush (Gore)",
                          "Bush (Kerry)",
                          "Obama (McCain)",
                          "Obama (Romney)",
                          "Trump (Clinton)"),
                 sigma_ratio = c(quantile(apply(p_anes_1972[, paste0("sigma[", 4:3, "]")], 1, function(x) x[2]/x[1]), 0.5),
                                quantile(apply(p_anes_1976[, paste0("sigma[", 4:3, "]")], 1, function(x) x[2]/x[1]), 0.5),
                                quantile(apply(p_anes_1980[, paste0("sigma[", 4:3, "]")], 1, function(x) x[2]/x[1]), 0.5),
                                quantile(apply(p_anes_1984[, paste0("sigma[", 4:3, "]")], 1, function(x) x[2]/x[1]), 0.5),
                                quantile(apply(p_anes_1988[, paste0("sigma[", 4:3, "]")], 1, function(x) x[2]/x[1]), 0.5),
                                quantile(apply(p_anes_1992[, paste0("sigma[", 3:4, "]")], 1, function(x) x[2]/x[1]), 0.5),
                                quantile(apply(p_anes_1996[, paste0("sigma[", 4:3, "]")], 1, function(x) x[2]/x[1]), 0.5),
                                quantile(apply(p_anes_2000[, paste0("sigma[", 3:4, "]")], 1, function(x) x[2]/x[1]), 0.5),
                                quantile(apply(p_anes_2004[, paste0("sigma[", 3:4, "]")], 1, function(x) x[2]/x[1]), 0.5),
                                quantile(apply(p_anes_2008[, paste0("sigma[", 4:3, "]")], 1, function(x) x[2]/x[1]), 0.5),
                                quantile(apply(p_anes_2012[, paste0("sigma[", 4:3, "]")], 1, function(x) x[2]/x[1]), 0.5),
                                quantile(apply(p_anes_2016[, paste0("sigma[", 3:4, "]")], 1, function(x) x[2]/x[1]), 0.5)),
                 lower90 = c(quantile(apply(p_anes_1972[, paste0("sigma[", 4:3, "]")], 1, function(x) x[2]/x[1]), 0.05),
                             quantile(apply(p_anes_1976[, paste0("sigma[", 4:3, "]")], 1, function(x) x[2]/x[1]), 0.05),
                             quantile(apply(p_anes_1980[, paste0("sigma[", 4:3, "]")], 1, function(x) x[2]/x[1]), 0.05),
                             quantile(apply(p_anes_1984[, paste0("sigma[", 4:3, "]")], 1, function(x) x[2]/x[1]), 0.05),
                             quantile(apply(p_anes_1988[, paste0("sigma[", 4:3, "]")], 1, function(x) x[2]/x[1]), 0.05),
                             quantile(apply(p_anes_1992[, paste0("sigma[", 3:4, "]")], 1, function(x) x[2]/x[1]), 0.05),
                             quantile(apply(p_anes_1996[, paste0("sigma[", 4:3, "]")], 1, function(x) x[2]/x[1]), 0.05),
                             quantile(apply(p_anes_2000[, paste0("sigma[", 3:4, "]")], 1, function(x) x[2]/x[1]), 0.05),
                             quantile(apply(p_anes_2004[, paste0("sigma[", 3:4, "]")], 1, function(x) x[2]/x[1]), 0.05),
                             quantile(apply(p_anes_2008[, paste0("sigma[", 4:3, "]")], 1, function(x) x[2]/x[1]), 0.05),
                             quantile(apply(p_anes_2012[, paste0("sigma[", 4:3, "]")], 1, function(x) x[2]/x[1]), 0.05),
                             quantile(apply(p_anes_2016[, paste0("sigma[", 3:4, "]")], 1, function(x) x[2]/x[1]), 0.05)),
                 upper90 = c(quantile(apply(p_anes_1972[, paste0("sigma[", 4:3, "]")], 1, function(x) x[2]/x[1]), 0.95),
                             quantile(apply(p_anes_1976[, paste0("sigma[", 4:3, "]")], 1, function(x) x[2]/x[1]), 0.95),
                             quantile(apply(p_anes_1980[, paste0("sigma[", 4:3, "]")], 1, function(x) x[2]/x[1]), 0.95),
                             quantile(apply(p_anes_1984[, paste0("sigma[", 4:3, "]")], 1, function(x) x[2]/x[1]), 0.95),
                             quantile(apply(p_anes_1988[, paste0("sigma[", 4:3, "]")], 1, function(x) x[2]/x[1]), 0.95),
                             quantile(apply(p_anes_1992[, paste0("sigma[", 3:4, "]")], 1, function(x) x[2]/x[1]), 0.95),
                             quantile(apply(p_anes_1996[, paste0("sigma[", 4:3, "]")], 1, function(x) x[2]/x[1]), 0.95),
                             quantile(apply(p_anes_2000[, paste0("sigma[", 3:4, "]")], 1, function(x) x[2]/x[1]), 0.95),
                             quantile(apply(p_anes_2004[, paste0("sigma[", 3:4, "]")], 1, function(x) x[2]/x[1]), 0.95),
                             quantile(apply(p_anes_2008[, paste0("sigma[", 4:3, "]")], 1, function(x) x[2]/x[1]), 0.95),
                             quantile(apply(p_anes_2012[, paste0("sigma[", 4:3, "]")], 1, function(x) x[2]/x[1]), 0.95),
                             quantile(apply(p_anes_2016[, paste0("sigma[", 3:4, "]")], 1, function(x) x[2]/x[1]), 0.95)))

G3$year_color <- ifelse(G3$year == 2016, G3$year, "Other")


# "As the second panel shows, error in the public’s placement of Trump is
# substantially greater than that of Clinton (1.37 times), and much larger
# than the average ratio of placement error between presidential candidates
# across all previous elections (1.26 times)." in main article.

# Ratio difference between Trump and Clinton 1.37
round(G1$sigma[G1$item == "Trump" & G1$year == 2016] / G1$sigma[G1$item == "Clinton" & G1$year == 2016], 2)
# How much larger is the Trump v. Clinton difference from the average
# difference? 1.26
round(G3$sigma_ratio[G3$item == "Trump (Clinton)"] / mean(G3$sigma_ratio[G3$item != "Trump (Clinton)"]), 2)

# Whether label goes to left or right of point estimate in the figure
G3$upper <- 1
G3$upper[G3$item == "Trump (Clinton)"] <- 0

G3_plot <- ggplot(G3, aes(y = year, x = sigma_ratio, fill = year_color, color = year_color)) +
  my.theme(borderless = 2, bordersize = 1, base_size = 10, tick_colour = "white", grid.x_colour = "grey85", grid.y_colour = "grey85") +
  coord_cartesian(xlim = c(0.93, 1.52)) +
  scale_x_continuous(breaks = c(1, 1.1, 1.2, 1.3, 1.4, 1.5),
                     labels = c("1", "1.1", "1.2", "1.3", "1.4", "1.5")) +
  scale_y_continuous(breaks = seq(1972, 2016, by = 4)) +
  labs(y = "", x = expression("Ratio of ideological placement error ("*sigma[j] / sigma[j^minute]*")"),
       title = "Ratio of Ideological Placement Error") +
  geom_vline(xintercept = 1, linetype = 1, size = 0.3, color = "grey50") +
  geom_segment(aes(y = year, yend = year, x = lower90, xend = upper90), lineend = "round", color = "grey20", size = 0.5) +
  geom_point(shape = 21, size = 3, stroke = 0.6) +
  geom_label(data = subset(G3, upper == 1), aes(y = year, x = sigma_ratio, label = item), hjust = 0.5, nudge_x = 0, nudge_y = 1.6, size = 2.7, color = "black", fill = "grey97", label.r = unit(0, "lines"), label.padding = unit(0.09, "lines"), label.size = NA) +
  geom_label(data = subset(G3, upper == 0), aes(y = year, x = sigma_ratio, label = item), hjust = 0.5, nudge_x = 0, nudge_y = 1.6, size = 2.7, color = "black", fill = "grey97", label.r = unit(0, "lines"), label.padding = unit(0.09, "lines"), label.size = NA) +
  scale_fill_manual(values = c("pink", "grey80")) +
  scale_color_manual(values = c("black", "grey40")) +
  theme(legend.position = "none",
        panel.background = element_rect(fill = "grey97", color = "white"),
        plot.margin = unit(c(0.5, 0.5, 0.5, -0.5), "lines"))

###### FIGURE 2
pdf("Graphs/Figure_2-RAW.pdf", 7, 5.5, useDingbats = FALSE)
grid.arrange(G1_plot, G3_plot, ncol = 2)
dev.off()

###### FIGURE A2
pdf("Graphs/Figure_A2-RAW.pdf", 3.75, 5.5, useDingbats = FALSE)
G2_plot
dev.off()


# APPENDIX C & FOOTNOTE 4: "This is not due to respondents not knowing how to
# respond as is common with, for example, third-party candidates: only 3% of
# respondents answer "Don’t know" when asked to place Trump; 2% when asked to
# place Clinton. See Appendix for major- and third-party comparisons."
round(G1$dk[G1$item == "Trump" & G1$year == 2016], 2)
round(G1$dk[G1$item == "Clinton" & G1$year == 2016], 2)

# TABLE A3
stargazer(subset(G1, label %in% 
                     c("Republican candidate",
                       "Democratic candidate",
                       "Anderson",
                       "Perot",
                       "Nader"))[, c("item", "year", "sigma", "sd", "dk")],
          summary = FALSE,
          rownames = FALSE)

# APPENDIX D: Ratio of Trump to Clinton with respect to sigma (1.37) and
# to the SD (1.15)
G1$sigma[G1$item == "Trump" & G1$year == 2016] / G1$sigma[G1$item == "Clinton" & G1$year == 2016]
G1$sd[G1$item == "Trump" & G1$year == 2016] / G1$sd[G1$item == "Clinton" & G1$year == 2016]

##### Figure A3
G1$year <- factor(G1$year, levels = rev(seq(1972, 2016, by = 4)))

pdf("Graphs/Figure_A3-RAW.pdf", 4, 5.5, useDingbats = FALSE)
ggplot(subset(G1, label %in% c("Democratic candidate", "Republican candidate")), aes(y = year, x = dk)) +
  my.theme(borderless = 2, base_size = 10, tick_colour = "white") +
  labs(x = "Proportion responding \"Don't know\"", y = "") +
  scale_x_continuous(limits = c(x = c(0, 0.38)),
                     breaks = c(0, 0.1, 0.2, 0.3),
                     labels = c("0", "0.1", "0.2", "0.3")) +
  scale_y_discrete() +
  geom_hline(yintercept = 2, colour = "grey95", size = 12) +
  geom_hline(yintercept = 4, colour = "grey95", size = 12) +
  geom_hline(yintercept = 6, colour = "grey95", size = 12) +
  geom_hline(yintercept = 8, colour = "grey95", size = 12) +
  geom_hline(yintercept = 10, colour = "grey95", size = 12) +
  geom_hline(yintercept = 12, colour = "grey95", size = 12) +
  geom_vline(xintercept = c(0, 0.1, 0.2, 0.3),
             colour = "grey90", size = 0.2) +
  geom_segment(data = subset(G1, label == "Democratic candidate"),
               aes(x = dk_lower90, xend = dk_upper90, y = year, yend = year), 
               size = 0.25, position = position_nudge(y = -0.17)) +
  geom_segment(data = subset(G1, label == "Republican candidate"),
               aes(x = dk_lower90, xend = dk_upper90, y = year, yend = year), 
               size = 0.25, position = position_nudge(y = 0.17)) +
  geom_segment(data = subset(G1, item %in% c("Anderson", "Perot", "Nader")),
               aes(x = dk_lower90, xend = dk_upper90, y = year, yend = year), 
               size = 0.25, position = position_nudge(y = 0)) +
  geom_point(data = subset(G1, label == "Democratic candidate"),
             size = 2.5, shape = 21, fill = "white", stroke = 0.5,
             position = position_nudge(y = -0.17)) +
  geom_point(data = subset(G1, label == "Republican candidate"),
             size = 2.5, shape = 21, fill = "white", stroke = 0.5,
             position = position_nudge(y = 0.17)) +
  geom_point(data = subset(G1, item %in% c("Anderson", "Perot", "Nader")),
             size = 2.5, shape = 21, fill = "black", stroke = 0.5,
             position = position_nudge(y = 0)) +
  geom_text(data = subset(G1, label == "Democratic candidate"), aes(x = dk_upper90, label = item),
            hjust = 0, size = 3, position = position_nudge(y = -0.17, x = 0.005)) +
  geom_text(data = subset(G1, label == "Republican candidate"), aes(x = dk_upper90, label = item),
            hjust = 0, size = 3, position = position_nudge(y = 0.17, x = 0.005)) +
  geom_text(data = subset(G1, item %in% c("Anderson", "Perot")), aes(x = dk_upper90, label = item),
            hjust = 0, size = 3, position = position_nudge(y = 0, x = 0.005)) +
  geom_text(data = subset(G1, item == "Nader"), aes(x = dk_lower90, label = item),
            hjust = 1, size = 3, position = position_nudge(y = 0, x = -0.005)) +
  theme(legend.position = "none",
        plot.margin = unit(c(0.5, 0.5, 0.5, -0.5), "lines"))
dev.off()

